package cn.agiledata.bank.myspace.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.account.service.AccountServiceFactory;
import cn.agiledata.bank.common.bean.CommonAction;
import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.service.AccountService;
import cn.agiledata.bank.common.service.BankFactory;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AccountMessage;
import cn.agiledata.bank.hibernateBean.MessageCustomization;
import cn.agiledata.bank.myspace.service.MessageCustomService;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;
import com.vnv.infrastructure.logic.IPBMgrFactory;

public class SetupAction extends CommonAction {

	public String getIfWrongReturnString() {
		// TODO Auto-generated method stub
		return this.returnString;
	}

	private MessageCustomService getMessageCustomService() {
		return (MessageCustomService) IPBAppFactory
				.getObj("messageCustomService");
	}

	private AccountTemplateInter getAccountTemplateInter() {
		return (AccountTemplateInter) IPBMgrFactory.getMgr("accountTemplate");
	}

	public ActionForward init(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		Set accountMessages = new HashSet();
		Set defaultMessages = new HashSet();
		Set finalMessages = new HashSet();
		// 判断用户是否已定制过消息
		Account account = initAccount(request);
		if (account.getAccountMessages() != null
				&& account.getAccountMessages().size() > 0) {
			Iterator iter = account.getAccountMessages().iterator();
			for (; iter.hasNext();) {
				AccountMessage am = (AccountMessage) iter.next();
				String[] message = new String[] {
						am.getMessageCustomization().getSetId().trim(),
						am.getEmailValidFlag().trim(),
						am.getMobileValidFlag().trim() };
				finalMessages.add(message);
			}
		} else {
			// 获得消息可定制消息
			List messageCustoms = new ArrayList();
			if (null != getMessageCustomService()) {
				messageCustoms = getMessageCustomService()
						.findAllMessageCustoms();
				Iterator iter = messageCustoms.iterator();
				for (; iter.hasNext();) {
					MessageCustomization mc = (MessageCustomization) iter
							.next();
					String[] message = new String[] { mc.getSetId().trim(),
							mc.getEmailDefault().trim(),
							mc.getMobileDefault().trim() };
					finalMessages.add(message);
				}
			}
		}

		request.setAttribute("finalMessages", finalMessages);
		request.setAttribute("account", account);

		return mapping.findForward("UserSetup");
	}

	private Account initAccount(HttpServletRequest request) throws IPBException {
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		AccountTemplateInter at = (AccountTemplateInter) IPBMgrFactory
				.getMgr("accountTemplate");
		Account account = at.getAccount(String.valueOf(accountId));
		// request.setAttribute("subAccounts",acco.getSubAccounts());
		return account;
	}

	public ActionForward save(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		Set finalMessages = new HashSet();
		// 获得页面数据
		String[] emails = request.getParameterValues("email_group");
		String[] mobiles = request.getParameterValues("mobile_group");

		// 获得用户对象
		Account account = getAccountTemplateInter().getAccount(
				String.valueOf(WeatherUtil.getAccountIdFromSession(request)));
		// 定制消息
		account = this.getMessageCustomService().saveAllMessageCustoms(emails,
				mobiles, account);
		Iterator ami = account.getAccountMessages().iterator();
		for (; ami.hasNext();) {
			AccountMessage accountMessage = (AccountMessage) ami.next();
			String[] message = new String[] {
					accountMessage.getMessageCustomization().getSetId().trim(),
					accountMessage.getEmailValidFlag().trim(),
					accountMessage.getMobileValidFlag().trim() };
			finalMessages.add(message);
		}
		// 判断用户是否已定制过消息
		/*
		 * if (account.getAccountMessages() != null &&
		 * account.getAccountMessages().size() > 0) { Iterator iter =
		 * account.getAccountMessages().iterator(); for (; iter.hasNext();) {
		 * AccountMessage am = (AccountMessage) iter.next(); int eChange = 0;
		 * for (int i = 0; i < (email == null ? 0 : email.length); i++) { if
		 * (am.getMessageCustomization().getSetId() .equals(email[i])) { eChange =
		 * 1; am.setEmailValidFlag("M".equals(am.getEmailValidFlag() .trim()) ?
		 * "M" : "Y"); } } if (eChange == 0) {
		 * am.setEmailValidFlag("M".equals(am.getEmailValidFlag() .trim()) ? "M" :
		 * "N"); } int mChange = 0; for (int i = 0; i < (mobile == null ? 0 :
		 * mobile.length); i++) { if
		 * (am.getMessageCustomization().getSetId().equals( mobile[i])) {
		 * mChange = 1; am.setMobileValidFlag("M".equals(am
		 * .getMobileValidFlag().trim()) ? "M" : "Y"); } } if (mChange == 0) {
		 * am.setMobileValidFlag("M".equals(am.getMobileValidFlag() .trim()) ?
		 * "M" : "N"); } String[] message = new String[] {
		 * am.getMessageCustomization().getSetId().trim(),
		 * am.getEmailValidFlag().trim(), am.getMobileValidFlag().trim() };
		 * finalMessages.add(message); } } else { // 获得消息可定制消息 List
		 * messageCustoms = new ArrayList(); if (null !=
		 * getMessageCustomService()) { messageCustoms =
		 * getMessageCustomService() .findAllMessageCustoms(); Iterator iter =
		 * messageCustoms.iterator(); for (; iter.hasNext();) {
		 * MessageCustomization mc = (MessageCustomization) iter .next();
		 * AccountMessage accountMessage = new AccountMessage();
		 * 
		 * accountMessage.setCreateTime(new Date());
		 * accountMessage.setCreateUser(account.getName());
		 * accountMessage.setUpdateTime(new Date());
		 * accountMessage.setUpdateUser(account.getName());
		 * 
		 * WeatherUtil.fillCreateAndUpdateInfo(accountMessage, account
		 * .getId().toString()); accountMessage.setMessageCustomization(mc);
		 * accountMessage.setVersion(new Long(1));
		 * accountMessage.setAccount(account); int eChange = 0; for (int i = 0;
		 * i < email.length; i++) { if (mc.getSetId().equals(email[i])) {
		 * eChange = 1; accountMessage.setEmailValidFlag("M".equals(mc
		 * .getEmailDefault().trim()) ? "M" : "Y"); } } if (eChange == 0) {
		 * accountMessage.setEmailValidFlag("M".equals(mc
		 * .getEmailDefault().trim()) ? "M" : "N"); } int mChange = 0; for (int
		 * i = 0; i < mobile.length; i++) { if (mc.getSetId().equals(mobile[i])) {
		 * mChange = 1; accountMessage.setMobileValidFlag("M".equals(mc
		 * .getMobileDefault().trim()) ? "M" : "Y"); } } if (mChange == 0) {
		 * accountMessage.setMobileValidFlag("M".equals(mc
		 * .getMobileDefault().trim()) ? "M" : "N"); }
		 * account.getAccountMessages().add(accountMessage); String[] message =
		 * new String[] { mc.getSetId().trim(),
		 * accountMessage.getEmailValidFlag().trim(),
		 * accountMessage.getMobileValidFlag().trim() };
		 * finalMessages.add(message); } } }
		 */
		request.setAttribute("finalMessages", finalMessages);
		request.setAttribute("account", account);
		BankFactory vf = new AccountServiceFactory();
		AccountService as = vf.getBankBean("Impl");
		WeatherUtil.fillUpdateInfo(account, account.getId().toString());//更新最后人员和日期
		as.saveAccountMessage(account);
		request.setAttribute("errorMsg", "保存成功");
		return mapping.findForward("UserSetup");
	}
}
